<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col :span="3" class="left">
        <a href="/">
          <img :src="logoUrl" />
        </a>
      </el-col>
      <el-col :span="15" class="center">
        <div class="wrapper">
          <el-input
            placeholder="搜索生鲜，水果或蔬菜"
            v-model="search"
            @focus="focus"
            @blur="blur"
            @input="input"
          />
          <button class="el-button el-button--primary">
            <i class="el-icon-search" />
          </button>
          <dl class="hotPlace" v-if="isHotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item, idx) in hotPlace" :key="idx">
              <a :href="'/products?keyword=' + encodeURIComponent(item)">{{
                item
              }}</a>
            </dd>
          </dl>
          <dl class="searchList" v-if="isSearchList">
            <dd v-for="(item, idx) in searchList" :key="idx">
              <a :href="'/products?keyword=' + encodeURIComponent(item.name)">{{
                item.name
              }}</a>
            </dd>
          </dl>
        </div>
        <p class="suggest">
          <a
            v-for="(item, idx) in hotPlace"
            :key="idx"
            :href="'/products?keyword=' + encodeURIComponent(item)"
            >{{ item }}</a
          >
        </p>
        <ul class="nav">
          <li><nuxt-link to="/" class="takeout">猜你喜欢</nuxt-link></li>
          <li><nuxt-link to="/" class="movie">当季优选</nuxt-link></li>
          <li><nuxt-link to="/" class="hotel">国际直采</nuxt-link></li>
          <li><nuxt-link to="/" class="apartment">大家在买</nuxt-link></li>
          <li><nuxt-link to="/" class="business">地域美食</nuxt-link></li>
        </ul>
      </el-col>
      <el-col :span="6" class="right iconfont">
        <ul class="security">
          <li>
            <i class="refund" />
            <p class="txt">送上门</p>
          </li>
          <li>
            <i class="single" />
            <p class="txt">坏了退</p>
          </li>
          <li>
            <i class="overdue" style="font-size: 26px"/>
            <p class="txt">极速达</p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from "lodash";
import { simpleSearch } from "@/api/esproduct";
export default {
  data() {
    return {
      logoUrl: require("@/assets/img/logo-h.png"),
      search: "",
      isFocus: false,
      hotPlace: [],
      searchList: []
    };
  },
  computed: {
    isHotPlace: function() {
      return this.isFocus && !this.search;
    },
    isSearchList: function() {
      return this.isFocus && this.search;
    }
  },
  created() {
    this.getHotPlace();
  },
  methods: {
    focus: function() {
      this.isFocus = true;
    },
    blur: function() {
      let self = this;
      setTimeout(function() {
        self.isFocus = false;
      }, 200);
    },
    input: _.debounce(async function() {
      let self = this;
      // let city=self.$store.state.geo.position.city.replace('市','')
      const { data } = await this.$axios.get(
        "es/esProduct/search/simple?pageNum=0&pageSize=10&keyword=" +
          self.search
      );
      self.searchList = data.data.list;
    }, 300),
    async getHotPlace() {
      let self = this;
      const { data } = await this.$axios.get(
        "es/esProduct/search?pageNum=0&pageSize=5&&sort=2"
      );
      let s = new Set();
      for (let i = 0; i < data.data.list.length; i++) {
        s.add(data.data.list[i].brandName);
      }
      self.hotPlace = s;
    }
    // sear(pageNum, pageSize, keyword) {
    //   console.log(keyword);
    //   simpleSearch(pageNum, pageSize, keyword)
    // }
  }
};
</script>

<style lang="scss">
@import "@/assets/css/font/iconfont.css";
</style>
